package com.junxonline.plat.service.auth.impl;

import com.google.gson.Gson;
import com.junxonline.plat.dao.model.User;
import com.junxonline.plat.dao.repository.UserRepository;
import com.junxonline.plat.dao.vo.UserVO;
import com.junxonline.common.exception.CommonBizException;
import com.junxonline.common.util.CommonUtils;
import com.junxonline.common.util.JWTUtils;
import com.junxonline.common.util.RedisUtils;
import com.junxonline.plat.service.auth.LoginService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;

@Service
@Transactional
public class LoginServiceImpl implements LoginService {

    private Logger logger = LogManager.getLogger();

    @Autowired
    private RedisUtils redisUtils;

    @Autowired
    private UserRepository userRepository;

    @Override
    public Map<String, Object> login(String username, String password) {

        User user = userRepository.findByUsername(username);
        if (CommonUtils.isEmpty(user)) {
            throw CommonBizException.ACCOUNT_IS_NOT_FINDED;
        }


        user = userRepository.findByUsernameAndPassword(user.getUsername(), password);

        if (CommonUtils.isEmpty(user)) {
            return null;
        }

        Map<String, Object> returnMap = new HashMap<String, Object>();
        // 生成Token
        String token = JWTUtils.sign(user.getUserId(), user.getUsername(), user.getPassword());

        // 将Token和用户信息存入Redis
        Gson gson = new Gson();
        UserVO userVO = new UserVO();
        userVO.setUserId(user.getUserId());
        userVO.setUsername(user.getUsername());
        userVO.setNickname(user.getNickname());
        userVO.setSex(user.getSex());
        userVO.setPhone(user.getPhone());
        userVO.setEmail(user.getEmail());
        userVO.setDescription(user.getDescription());
        userVO.setCreateTime(user.getCreateTime());
        userVO.setLastLoginTime(user.getLastLoginTime());
        userVO.setLastLoginIp(user.getLastLoginIp());
        userVO.setStatus(user.getStatus());

        try {
            redisUtils.put("userId", "user-cache-" + token, userVO.getUserId().toString());
            redisUtils.put("userInfo", "user-cache-" + token, gson.toJson(userVO));
            redisUtils.put("userToken", "user-cache-" + token, token);

            logger.info("redis -> userId :" + redisUtils.get("userId", "user-cache-" + token));
            logger.info("redis -> userInfo :" + redisUtils.get("userInfo", "user-cache-" + token));
            logger.info("redis -> userToken :" + redisUtils.get("userToken", "user-cache-" + token));

        } catch (Exception e) {
            throw CommonBizException.REDIS_IS_NOT_CONNECTION;
        }

        returnMap.put("userId", userVO.getUserId());
        returnMap.put("userInfo", userVO);
        returnMap.put("token", token);

        return returnMap;

    }
}
